﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class PersonNameControl : UserControl
    {
        FATCASchema.Wrappers.PersonName _item;

        public PersonNameControl()
        {
            InitializeComponent();
        }

        public void Intitalize(FATCASchema.Wrappers.PersonName item)
        {
            NameItem = item;
        }

        public FATCASchema.Wrappers.PersonName NameItem
        {
            get { return _item; }
            set
            {
                _item = value;
                DataBind();
            }
        }

        private void DataBind()
        {
            txtFirst.DataBindings.Clear();
            txtFirst.DataBindings.Add("Text", NameItem, "FirstName");

            txtGeneralSuffix.DataBindings.Clear();
            txtGeneralSuffix.DataBindings.Add("Text", NameItem, "GeneralSuffix");

            txtLast.DataBindings.Clear();
            txtLast.DataBindings.Add("Text", NameItem, "LastName");

            txtPrecedingTitle.DataBindings.Clear();
            txtPrecedingTitle.DataBindings.Add("Text", NameItem, "PrecedingTitle");

            txtPrefix.DataBindings.Clear();
            txtPrefix.DataBindings.Add("Text", NameItem, "NamePrefix");

            genericTitleBindingSource.DataSource = NameItem.Title;
            genericMiddleBindingSource.DataSource = NameItem.MiddleName;
            genericGenerationBindingSource.DataSource = NameItem.GenerationIdentifier;
            genericSuffixBindingSource.DataSource = NameItem.Suffix;

            var nameType = NameItem.NameType;
            cmbNameType.DataBindings.Clear();
            cmbNameType.DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(OECDNameType_EnumType));
            cmbNameType.ValueMember = "Key";
            cmbNameType.DisplayMember = "Value";
            cmbNameType.DataBindings.Add("SelectedValue", NameItem, "NameType");
            cmbNameType.SelectedValue = nameType;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ParentForm.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.ParentForm.DialogResult = DialogResult.OK;
        }

        private void cmbNameType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNameType.SelectedValue is OECDNameType_EnumType)
                NameItem.NameType = (OECDNameType_EnumType)cmbNameType.SelectedValue;
        }

    }
}
